package com.ubiquando.api.server.db;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import com.google.appengine.api.datastore.Key;

@Entity
public class Client /*implements Comparable<Client>*/{

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Key id;
	
	private String name;
	
	private String email;
	
	private String pwd;
	
	private String location;
	
	private String registrationID;
	
//	private String clientLoginToken;
	
	private boolean connected; 

//	@ManyToMany(mappedBy = "id" )
//	private Set<Client> contacts;
	//@JoinTable
	//@ElementCollection/*(fetch=FetchType.EAGER)*/
	@Basic
	private Set<String> contacts;
	
//	@ManyToMany(mappedBy = "id")
//	private Set<Event> events;
	//@JoinTable
	//@ElementCollection/*(fetch=FetchType.EAGER)*/
//	@Basic
//	private Set<String> events
	
	@Basic
	private Set<String> meetingEvents;// = new ArrayList<String>();
	
	@Basic
	private Set<String> pickupEvents;
	
	public Client(){
		this.contacts = new HashSet<String>();
		this.meetingEvents = new HashSet<String>();
		this.pickupEvents = new HashSet<String>();
	}
	
//	public Client(String name, String location, String regID, String GToken){
//		this.name = name;
//		this.location = location;
//		this.registrationID = regID;
//		this.clientLoginToken = GToken;
//		this.connected = true;
//		this.contacts = new HashSet<Client>();
//		this.events = new HashSet<Event>();
//	}
	
	public Key getId(){
		return this.id;
	}

	public String getName(){
		return this.name;
	}
		
	public void setName(String name){
		this.name = name;
	}
	
	public String getLocation(){
		return this.location;
	}
	
	public void setLocation(String location){
		this.location = location;
	}
	
	public String getRegistrationID(){
		return this.registrationID;
	}
	
	public void setRegistrationID(String regID){
		this.registrationID = regID;
	}
	
	public boolean isConnected(){
		return this.connected;
	}
	
	public void setConnected(boolean connection){
		this.connected = connection;
	}
	
//	public String getClientLoginToken(){
//		return this.clientLoginToken;
//	}
//	
//	public void setClientLoginToken(String GToken){
//		this.clientLoginToken = GToken;
//	}
	
	public Set<String> getContacts(){
		return this.contacts;
	}
	
	public void setContacts(Set<String> contacts){
		this.contacts = contacts;
	}
	
	public Set<String> getMeetingEvents() {
		return meetingEvents;
	}
	
	public void setMeetingEvents(Set<String> meetingEvents) {
		this.meetingEvents = meetingEvents;
	}
	
	public Set<String> getPickupEvents() {
		return pickupEvents;
	}
	
	public void setPickupEvents(Set<String> pickupEvents) {
		this.pickupEvents = pickupEvents;
	}
	
//	public Set<String> getEvents(){
//		return this.events;
//	}
//	
//	public void setEvents(Set<String> events){
//		this.events = events;
//	}

//	@Override
//	public int compareTo(Client other) {
//		long diff = this.id - other.id;
//		if(diff < 0)
//			return -1;
//		if(diff > 0)
//			return 1;
//		return 0;
//	}
	
//	public boolean equals(Client other){
//		if(this.id.equals(other.getId()))
//			return true;
//		return false;
//	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getPwd() {
		return pwd;
	}
	
	public void addContact(String clientId){
		if(this.contacts == null)
			this.contacts = new HashSet<String>();
		this.contacts.add(clientId);
	}
	
//	public void addEvent(String eventId){
//		if(this.events == null)
//			this.events = new HashSet<String>();
//		this.events.add(eventId);
//	}
	
	public void addEvent(String eventId, int eventType){
//		if(this.events == null)
//			this.events = new HashSet<String>();
		if(eventType == Event.MEETING_EVENT)
			this.meetingEvents.add(eventId);
		else if(eventType == Event.PICKUP_EVENT)
			this.pickupEvents.add(eventId);
	}

	public void removeEvent(String eventId, int eventType) {
//		if(this.events != null)
//			this.events.remove(eventId);
		if(eventType == Event.MEETING_EVENT)
			this.meetingEvents.remove(eventId);
		else if(eventType == Event.PICKUP_EVENT)
			this.pickupEvents.remove(eventId);
	}
	
//	public Set<String> getEvents(){
//		Set<String> events = new HashSet<String>();
//		events.addAll(this.meetingEvents);
//		events.addAll(this.pickupEvents);
//		return events;
//	}
	
}
